<template>
  <el-dialog
      :title="config.title"
      :visible="visible"
      :close-on-click-modal="false"
      v-el-drag-dialog
      @close="onClose"
      @open="onOpen"
      width="400px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="菜单标题" prop="title">
        <el-input v-model="form.title"/>
      </el-form-item>
      <el-form-item label="菜单路径" prop="path">
        <el-input v-model="form.path"/>
      </el-form-item>
      <el-form-item  label="路径名称" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item v-if="parentId==='root'" label="图标" prop="icon">
        <el-input v-model="form.icon"/>
      </el-form-item>
      <el-form-item label="页面组件路径" prop="componentPath">
        <el-input v-model="form.componentPath"/>
      </el-form-item>
      <el-form-item label="是否隐藏" prop="hidden">
        <el-switch v-model="form.hidden"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import sysMenuApi from '@/api/sysMenu'

  export default {
    props: {
      parentId: {},
      visible: {
        type: Boolean,
        default: false
      },
      row: {
        type: Object,
        default: function() {
          return null
        }
      },
      callback: {
        type: Function,
        default: function() {
          if (this.$parent.$refs.dataTable) {
            this.$parent.$refs.dataTable.getDataList()
          }
        }
      }
    },
    data() {
      return {
        form: {
          sortIndex: '',
          path: '',
          name: '',
          title: '',
          icon: '',
          componentPath: '',
          hidden: false
        },
        rules: {},
        config: {},
        configMap: {
          add: { api: 'add', title: '添加菜单' },
          edit: { api: 'update', title: '修改菜单' }
        }
      }
    },
    methods: {
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            sysMenuApi[this.config.api]({ ...this.form, parentId: this.parentId, type: 1 }).then(res => {
              this.callback()
              this.onClose()
              this.$notify({
                title: '成功',
                message: this.config.title + '成功',
                type: 'success'
              })
            })
          }
        })
      },
      getData() {
        for (let k in this.form) {
          this.form[k] = this.row[k]
        }
        this.form.id = this.row.id
      },
      onOpen() {
        if (this.row) {
          this.config = this.configMap['edit']
          this.$nextTick(() => {
            this.getData()
          })
        } else {
          this.config = this.configMap['add']
        }
      },
      onClose() {
        this.$refs.form.resetFields()
        this.$emit('update:visible', false)
      }
    },
    mounted() {
    }
  }
</script>

<style scoped>

</style>
